//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    root: '',
    siteroot: app.setting.siteroot,
    att_url: app.setting.att_url,
    action: 'home',
    pagetitle: '客服',

  },
  onReady: function () {
    //获得粉丝消息弹窗(需要时开启)
  },
  onShow: function (){
		var ssid=wx.getStorageSync('ssid');
		if(!ssid && !app.issession){
			app.util.session();
		}
	},
  onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (opt){
		var page = this;
		if(opt && opt.isgoshare){
				page.setData({
					ShowGoshare:true,
				});
		}
		wx.setNavigationBarTitle({ 'title': page.data.pagetitle });
		var route=page.route;
			page.setData({
				route:route,
			});
			if(route){
			var path=route.slice(0,-6).split('/');
			app.access.plugin=path.shift();
			app.access.action=path.join('.');
		}
		if(opt){
			app.access.query =app.util.made_query(opt,true);
			app.access.op=opt.op?opt.op:'';
		}
		app.util.get(app.access, function (pagedata) {
			  console.log('kefu');console.log(pagedata);
			  page.setData({
				pagedata: pagedata,
				ShowAuthM:pagedata.show_setmember==2?true:false,
			  });
		});
  },
  lianxikefu: function(e) {
		var the=this;
		var pagedata=the.data.pagedata
		var data = e.currentTarget.dataset;
		var item = pagedata.list[data.index];
		//企业微信
		if(item.type==8){
			wx.openCustomerServiceChat({
			  extInfo: {url: item.worker.appurl},
			  corpId: item.worker.corpId,
			  success(res) {
				  console.log(res)
			  },
			  fail(res) {
				  console.log('fail'); console.log(res)
				  app.util.message(res.errMsg);
			  }
			})
		}
   },
  lianxikefu8: function(e) {
		var the=this;
		var pagedata=the.data.pagedata
		//企业微信
		if(pagedata.type==8){
			wx.openCustomerServiceChat({
			  extInfo: {url: pagedata.linkurl},
			  corpId: pagedata.corpId,
			  success(res) {
				  console.log(res)
			  },
			  fail(res) {
				  console.log('fail'); console.log(res)
				  app.util.message(res.errMsg);
			  }
			})
		}
   },
  goback: function() {
		var pages=getCurrentPages()
		if(pages.length>1){
			wx.navigateBack({
				'delta':1,
				'fail':function(){
					//console.log('333');
					app.util['access'](app.plugin_name+'/index/');
				},
			})
		}else{
			app.util['access'](app.plugin_name+'/index/');
		}
   },
  showqr:function(e){
	  var the=this
	  var data = e.currentTarget.dataset;
	  var pagedata=the.data.pagedata
	  pagedata.getmember=pagedata.list[data.index]
	  if(pagedata.getmember.type==9){
		pagedata.getmember.kefuqr=pagedata.getmember.guwenkfewm
	  }
	  the.setData({
         'pagedata.getmember':  pagedata.getmember,
		 showqr:true
      });
	  
  },
  closeqr:function(){
	  var the=this
	  the.setData({
         showqr: false,
      });
	  
  },
   handleContact: function (e) {
        console.log(e.path)
        console.log(e.query)
   },
   makePhoneCall: function(e){
	   var the=this
		var data = e.currentTarget.dataset;
		wx.makePhoneCall({
		  phoneNumber: data.phone
		})
   },
  onChooseAvatar:function(e){
		//获取头像
		var the=this;
		var avatarUrl= e.detail.avatarUrl
		the.setData({
			avatarUrl:avatarUrl
		})
		app.avatarUrl=avatarUrl
	},
	info_login:function(e){
		//根据openid为用户登陆，此处尝试获取用户信息
		var the=this;
		app.util.getUserInfo(e,function(res){
			if(res && res.userInfo){
				app.isgetUserInfo=true
				the.setData({
					ShowAuthM:false
				})
				the.onLoad();
			}else{
				return false;
			}
		});
	},
	mobile_login:function(e){
		//根据openid为用户登陆，此处尝试获取用户手机号
		var the = this
		var pagedata= the.data.pagedata
		app.util.post('core/member.login/wxapp_login/return_mobile=1',e.detail,function(res){
			the.setData({
				ShowAuthM:false
			})
		});
	},
  closeAuthM:function(){
		var the=this;
		the.setData({
			ShowAuthM:false
		})
	},
  click: function (evt) {
    //用户点击事件 data.func 为方法名，data.params 为调用参数！
    var data = evt.currentTarget.dataset;
    app.util[data.func](data.params);
  },
  click_tab:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util.tab(data.params);
	},
  formSubmit: function (event) {
    var data = event.detail.value;
    app.util.post('post', data);
  },
  onShareAppMessage: function (res) {
		var the = this
		var pagedata = the.data.pagedata
		if (res.from === 'button') {
		}
		var path=the.data.route+'?'+app.access.query;
		if(app.access.op){
			path +='&op='+app.access.op;
		}
		if(pagedata.fromuser){
			path +='&fromuser='+pagedata.fromuser;
		}
		return {
		  title: the.data.pagetitle,
		  path: path,
		  desc: app.setting.sharedesc,
		  success: function(res) {
		  },
		  fail: function(res) {
		  }
		}
	},
	onShareTimeline: function (res) {
		var the = this
		var pagedata = the.data.pagedata
		var query=app.access.query
		if(app.access.op){
			query +='&op='+app.access.op;
		}
		if(pagedata.fromuser){
			query +='&fromuser='+pagedata.fromuser;
		}
		query +='&isgoshare=1'
		return {
		  title: the.data.pagetitle,
		  query: query,
		  desc: app.setting.sharedesc,
		  success: function(res) {
		  },
		  fail: function(res) {
		  }
		}
	},
});